package calin.podcast;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class CalinPodcastSettingsBrowser extends ListActivity {
	private List<String> itemPath = new ArrayList<String>();
	private List<String> itemName = new ArrayList<String>();
	private String pathKey = "path";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.podcasts_settings_browser);
			setTitle(getString(R.string.app_name) + " "
					+ getString(R.string.settings_browser_title));

			ListView list = (ListView) findViewById(android.R.id.list);
			list.setAdapter(getFileList());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayAdapter<String> getFileList() {
		String path = null;
		ArrayAdapter<String> podcasts = null;
		try {
			Bundle bundle = getIntent().getExtras();

			if (!bundle.isEmpty() && bundle.containsKey(pathKey))
				path = bundle.getString(pathKey);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			if (path == null)
				path = ((Podcasts) this.getApplication()).getPodcastDirectory(CalinPodcastSettingsBrowser.this)
						.getAbsolutePath();
			File directory = ((Podcasts) this.getApplication())
					.getDirectory(path);
			
			FilenameFilter filefilter = new FilenameFilter() {

				public boolean accept(File dir, String name) {
					File fileItem = new File(dir.getAbsolutePath() + "/" + name);

					if (fileItem.isDirectory())
						return true;

					return false;
				}
			};

			if (directory.getParentFile().canWrite()) {
				itemPath.add(directory.getParentFile().getAbsolutePath());
				itemName.add("..");
			}

			File[] dirs = directory.listFiles(filefilter);
			for (int i = 0; i < dirs.length; i++) {
				File dir = dirs[i];

				itemPath.add(dir.getAbsolutePath());
				itemName.add(dir.getName());
			}
			filefilter = new FilenameFilter() {

				public boolean accept(File dir, String name) {
					File fileItem = new File(dir.getAbsolutePath() + "/" + name);

					if (fileItem.isDirectory())
						return false;

					return true;
				}
			};
			File[] files = directory.listFiles(filefilter);
			for (int i = 0; i < files.length; i++) {
				File file = files[i];

				itemPath.add(file.getAbsolutePath());
				itemName.add(file.getName());
			}

			Button selectButton = (Button) findViewById(R.id.settings_select_button);
			Button cancelButton = (Button) findViewById(R.id.settings_cancel_button);
			selectButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View view) {
					Bundle bundle = getIntent().getExtras();

					((Podcasts) getApplicationContext()).setSettings(
							"podcasts_path", bundle.getString(pathKey), CalinPodcastSettingsBrowser.this);
					Intent i = new Intent(CalinPodcastSettingsBrowser.this,
							CalinPodcastSettings.class);
					startActivityForResult(i,
							Podcasts.ACTIVITY_SETTINGS_BROWSER);
				}

			});
			cancelButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View view) {
					Intent i = new Intent(CalinPodcastSettingsBrowser.this,
							CalinPodcastSettings.class);
					startActivityForResult(i,
							Podcasts.ACTIVITY_SETTINGS_BROWSER);
				}

			});

			// ListView list = (ListView) findViewById(R.id.podcasts_list);
			podcasts = new ArrayAdapter<String>(this, R.layout.podcasts_row,
					itemName);

			// if(selectButton != null)
			// getListView().addFooterView(selectButton);
			// if(cancelButton != null)
			// getListView().addFooterView(cancelButton);
			// list.setAdapter(podcasts);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return podcasts;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		if (!(new File(itemPath.get((int) id))).isDirectory())
			return;

		Intent intent = new Intent(this, CalinPodcastSettingsBrowser.class);
		Bundle bundle = new Bundle();
		bundle.putString(pathKey, itemPath.get((int) id));

		intent.putExtras(bundle);
		startActivity(intent);
		finish();
	}

}